﻿namespace DotNetNuke.Modules.DNNWeather
{
    using DotNetNuke.Modules.DNNWeather.Components;
    using DotNetNuke.Services.Localization;
    using DotNetNuke.UI.Skins;
    using System;
    using System.Web.UI;

    /// <summary>
    /// This is the skin object class
    /// </summary>
    public partial class ViewWeatherSO : SkinObjectBase
    {
        private const string STR_CityId = "CityId";
        private const string STR_CityName = "CityName";
        private const string STR_Template = "Template";
        private const string STR_DegreesUnits = "DegreesUnits";
        private const string UserControlName = "ViewWeatherSO.ascx";
        private const string STR_PRE_004VersionPATH = "Documentation";
        private const string STR_AFTER_003VersionNEWPATH = "Files";

        #region "Properties"

        /// <summary>
        /// Property used for the city being shown
        /// </summary>
        public string CityId
        {
            get
            {
                if (!(ViewState[STR_CityId] == null ||
                    ViewState[STR_CityId].ToString().Equals(string.Empty)))
                {
                    return ViewState[STR_CityId].ToString();
                }
                throw new ParameterValidationException(
                            Localization.GetString("WarningCityIdRequired", Localization.GetResourceFile(this, UserControlName)));
            }
            set
            {
                ViewState[STR_CityId] = value;
            }
        }

        /// <summary>
        /// Property used for the city name being shown
        /// </summary>
        public string CityName
        {
            get
            {
                if (!string.IsNullOrEmpty(CityId))
                {
                    if (!(ViewState[STR_CityName] == null ||
                        ViewState[STR_CityName].ToString().Equals(string.Empty)))
                    {
                        return ViewState[STR_CityName].ToString();
                    }
                }
                throw new ParameterValidationException(
                            Localization.GetString("WarningCityNameRequired", Localization.GetResourceFile(this, UserControlName)));
            }
            set
            {
                ViewState[STR_CityName] = value;
            }
        }

        /// <summary>
        /// Property used for the weather template
        /// </summary>
        public string Template
        {
            get
            {
                if (!(ViewState[STR_Template] == null ||
                    ViewState[STR_Template].ToString().Equals(string.Empty)))
                {
                    return ViewState[STR_Template].ToString();
                }
                return string.Empty;
            }
            set
            {
                ViewState[STR_Template] = value;
            }
        }

        /// <summary>
        /// Property used for the degrees units
        /// </summary>
        public DNNWeather.Settings.DegreesUnits DegreeUnits
        {
            get
            {
                if (!(ViewState[STR_DegreesUnits] == null ||
                      ViewState[STR_DegreesUnits].ToString().Equals(string.Empty)))
                {
                    DNNWeather.Settings.DegreesUnits degreesUnit = DNNWeather.Settings.DegreesUnits.c;

                    if (Enum.TryParse<DNNWeather.Settings.DegreesUnits>(ViewState[STR_DegreesUnits].ToString(), out degreesUnit))
                        return degreesUnit;
                    else
                        throw new ParameterValidationException(
                            Localization.GetString("WarningInvalidDegreeUnits", Localization.GetResourceFile(this, UserControlName)));
                }
                return Settings.DegreesUnits.c;
            }
            set
            {
                ViewState[STR_DegreesUnits] = value;
            }
        }

        #endregion "Properties"

        #region "Events"

        /// <summary>
        /// Handles the Init event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Init(object sender, EventArgs e)
        {
            try
            {
                string cityId = CityId, cityName = CityName;
                DNNWeather.Settings.DegreesUnits degreeUnits = DegreeUnits;

                //Load the weather control into the skin object
                View viewWeather = (View)Page.LoadControl(this.AppRelativeTemplateSourceDirectory + "/View.ascx");
                viewWeather.CityId = cityId;
                viewWeather.CityName = cityName;
                //Change made from version 0.0.3 to 0.0.4 refering change do templates path from Documentation to Files
                viewWeather.Template = Template.Replace(STR_PRE_004VersionPATH, STR_AFTER_003VersionNEWPATH);
                viewWeather.DegreeUnits = degreeUnits.ToString();
                this.Controls.Add(viewWeather);
            }
            catch (ParameterValidationException exc)
            {
                this.Controls.Add(new LiteralControl(exc.Message));
            }
        }

        #endregion "Events"
    }
}